﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import flash.utils.*;

    public class StateMachine extends EventDispatcher {

        protected var m_states:Array;

        public function StateMachine(){
            this.m_states = [];
            super();
            this.m_states = [];
        }
        public function addState(_arg1:State, _arg2:Boolean=false):Boolean{
            var _local6:Array;
            var _local3:State = this.getState();
            var _local4:int = State.NORMAL_INTERRUPT;
            var _local5:Boolean;
            if (_arg2){
                _local6 = [_arg1];
                this.m_states = _local6.concat(this.m_states);
                _arg1.added();
                if (_local3 == null){
                    _arg1.enter();
                };
                _local5 = true;
            } else {
                if (_local3){
                    _local4 = _local3.getInterrupt();
                    if (_local4 != State.NO_INTERRUPT){
                        if (_local4 == State.FULL_INTERRUPT){
                            this.removeState(_local3);
                        } else {
                            _local3.exit();
                        };
                        _local5 = true;
                    };
                };
                if (_local4 != State.NO_INTERRUPT){
                    this.m_states.push(_arg1);
                    _arg1.added();
                    _arg1.enter();
                    _local5 = true;
                };
            };
            if (_local5){
                dispatchEvent(new Event(Event.CHANGE));
            };
            if (_arg1){
                GlobalEngine.log("StateMachine", (((("Add: " + getQualifiedClassName(_arg1)) + " (states: ") + this.m_states.length) + ")"));
            };
            return (_local5);
        }
        public function removeState(_arg1:State):void{
            var _local4:State;
            var _local2:State = this.getState();
            var _local3:int;
            while (_local3 < this.m_states.length) {
                if (this.m_states[_local3] == _arg1){
                    if (this.m_states[_local3] == _local2){
                        this.m_states[_local3].exit();
                    };
                    _arg1.removed();
                    this.m_states.splice(_local3, 1);
                    _local4 = this.getState();
                    if (((_local4) && (!((_local4 == _local2))))){
                        _local4.reenter();
                    };
                    dispatchEvent(new Event(Event.CHANGE));
                    break;
                };
                _local3++;
            };
            if (_arg1){
                GlobalEngine.log("StateMachine", (((("Remove: " + getQualifiedClassName(_arg1)) + " (states: ") + this.m_states.length) + ")"));
            };
        }
        public function removeAllStates():void{
            var _local3:State;
            var _local1:State = this.getState();
            if (_local1){
                _local1.exit();
                dispatchEvent(new Event(Event.CHANGE));
            };
            var _local2:int;
            while (_local2 < this.m_states.length) {
                _local3 = (this.m_states[_local2] as State);
                _local3.removed();
                _local2++;
            };
            this.m_states = [];
            GlobalEngine.log("StateMachine", "Remove all states");
        }
        public function getState():State{
            var _local1:State;
            if (this.m_states.length){
                _local1 = this.m_states[(this.m_states.length - 1)];
            };
            return (_local1);
        }
        public function getStates():Array{
            return (this.m_states);
        }
        public function popState():void{
            if (this.m_states.length){
                this.removeState((this.m_states[(this.m_states.length - 1)] as State));
            };
        }
        public function update(_arg1:Number):void{
            var _local2:State = this.getState();
            if (_local2){
                _local2.update(_arg1);
            };
        }

    }
}//package Engine.Classes 
